<?php

class Paginator extends Block{

	public $templateFile = "Paginator.tpl.std.html";
	public $templatePath = __DIR__ ;
	public $control = "PageNumber";
	public $pageNumber=1;
	public $pageLimit=5;
	public $pageSize=10;
	public $total=100;

	public function initialize(){
		
		if(isset($_GET[$this->control])){
			
			$this->pageNumber=$_GET[$this->control];
		}
	}
	
	public function show(){
		
		$this->parseFromFile( $this->templatePath . "/". $this->templateFile);
		
		$out="";
		$w=0;
		
		
		
		if($this->pageNumber <= $this->total ){
			$offset = 1;
		
			$offset = $this->pageNumber-1-($this->pageNumber-1)%($this->pageLimit);
		}
		
		if($this->pageNumber+$this->pageLimit >  $this->total  ){
			$offset = 	$this->total-$this->pageLimit;
		}
		
				
		for($i=1+$offset;  $i<=$this->pageLimit+$offset; $i++){
			
			//Si, está en la página actual, muestra otro formato.
			if($i==$this->pageNumber && isset($this->currentPage)){
				$this->currentPage->pageNumber=$i;
				$this->currentPage->control=$this->control;				
				$out.=$this->currentPage->replace();
			//Genera las otras paginas.
			}elseif(isset($this->page)){
				$this->page->pageNumber = $i;
				$this->page->control=$this->control;
				$out.=$this->page->replace();
			}
		}
		
		
		$this->page->block=$out;
		$this->currentPage="";
		
		//Ir a la primera pagina
		if(isset($this->first)){
			//Esta habilitado si es cualquier página.
			if($this->pageNumber > 1){
				$this->first->control=$this->control;
				$this->first->pageNumber=1;
				$this->firstDisabled->pageNumber=1;
				$this->firstDisabled->block="";
			//Deshabilitar el link si la pagina es ya la primera.	
			}else{
				$this->firstDisabled->pageNumber=1;
				$this->first->block="";
				
			}
		}
		//Ir a la página previa.
		if(isset($this->prev)){
			if($this->pageNumber > 1){
				$this->prev->pageNumber=$this->pageNumber-1;
				$this->prev->control=$this->control;
				$this->prevDisabled->block="";
			}else{
				$this->prev->block="";
			}
		}
		//Ir a la próxima página.
		if(isset($this->next)){
			if($this->pageNumber < $this->total){
				$this->next->pageNumber=$this->pageNumber+1;
				$this->next->control=$this->control;
				$this->nextDisabled->block="";
			}else{
				$this->next->block="";
			}
		}
		//Ir a la última página.
		if(isset($this->last)){
			
			if($this->pageNumber < $this->total){
				$this->last->pageNumber=$this->total;
				$this->last->control=$this->control;
				$this->lastDisabled->block="";
				
			//Si es la última pagina desabilitar
			}else{
				$this->last->pageNumber=$this->total;
				$this->lastDisabled->pageNumber=$this->total;
				$this->last->block="";
			}
		}
		
		
		
		return $this->replace();
	}

}


?>